<?php

/**
 * 消息群发
 */
class wGmess extends Controller
{

    const TPL = './views/wdminpage/';

    /**
     * 素材列表
     */
    public function getGmessList() {
        $this->loadModel('mGmess');
        $keyword = $this->pGet('key');
        $page    = intval($this->pGet('page'));
        $list    = $this->mGmess->getGmessList($page, 20, $keyword, false);
        $count   = $this->mGmess->getGmessCount();
        $this->echoJson([
            'list' => $list,
            'count' => $count
        ]);
    }

    /**
     * ajax删除素材
     */
    public function ajaxDelByMsgId() {
        $this->loadModel('mGmess');
        $id = intval($this->post('msgid'));
        $this->echoJson(array('status' => $this->mGmess->deleteGmess($id) === false ? 0 : 1));
    }

    /**
     * 编辑素材
     * @param type $Query
     */
    public function gmess_edit($Query) {
        if (isset($Query->id) && is_numeric($Query->id) && $Query->id > 0) {
            $this->Db->cache = false;
            $id              = intval($Query->id);
            $this->loadModel('mGmess');
            $gmess = $this->mGmess->getGmess($id);
            $this->assign('g', $gmess);
        } else {
            $id = 0;
        }
        $this->assign('ed', $id > 0);
        $this->show(self::TPL . 'gmess/gmess_edit.tpl');
    }

    /**
     * 【云搜索】
     * 获取分类列表
     * @see http://apistore.baidu.com/apiworks/servicedetail/863.html
     * @todo cache
     */
    public function getCloudCategorys() {
        $apis = "http://apis.baidu.com/showapi_open_bus/weixin/weixin_article_type";
        $ret  = json_decode(Curl::getWithHeader($apis, 'apikey: d5c983389f7bac5b1884f7ca82ac8247'));
        $cats = $ret->showapi_res_body->typeList;
        foreach ($cats as $cat) {
            $cat->id = intval($cat->id);
        }
        $cats = array_reverse($cats);
        $this->echoJson($cats);
    }

    /**
     * 【云搜索】
     * 获取列表
     * @param type $id
     * @param type $keyword
     * @param type $page
     * @see http://apistore.baidu.com/apiworks/servicedetail/863.html
     * @todo cache
     */
    public function getCloudList() {
        $id      = $this->pPost('id');
        $keyword = $this->pPost('key');
        $page    = intval($this->pPost('page')) + 1;
        if (!empty($keyword)) {
            $keyword = urlencode($keyword);
        }
        $apis = "http://apis.baidu.com/showapi_open_bus/weixin/weixin_article_list?typeId=$id&key=$keyword&page=$page";
        $ret  = json_decode(Curl::getWithHeader($apis, 'apikey: d5c983389f7bac5b1884f7ca82ac8247'));
        $this->echoJson($ret->showapi_res_body->pagebean);
    }

    /**
     * 素材克隆
     * @param string $url
     * @param string $contentImg
     */
    public function cloneGmess() {

        $url    = $this->pPost('url');
        $catimg = $this->pPost('contentImg');
        $data   = Curl::get($url);
        $html   = new simple_html_dom();
        $html->load($data);
        $rich_media_title = strip_tags($html->find('h2[class=rich_media_title]', 0));
        $js_content       = $html->find('div[id=js_content]', 0);

        if (!empty($rich_media_title) && !empty($js_content)) {
            $this->loadModel('mGmess');
            $desc       = trim(mb_substr(strip_tags($js_content), 0, 100, 'utf-8'));
            $js_content = trim($js_content);
            if ($this->mGmess->alterGmess(0, $rich_media_title, $js_content, $desc, $catimg)) {
                $this->echoSuccess();
            } else {
                echo $this->Db->getErrorInfo();
                $this->echoFail();
            }
        } else {
            $this->echoFail();
        }

    }

}